using UnityEngine;
using UndergroundSurvival.UI;

namespace UndergroundSurvival.Managers
{
    public class GameSceneManager : MonoBehaviour
    {
        [Header("Manager References")]
        [SerializeField] private BuildingManager buildingManager;
        [SerializeField] private ResourceManager resourceManager;
        [SerializeField] private RoomPrefabManager roomPrefabManager;
        [SerializeField] private ResourceTransportManager resourceTransportManager;

        [Header("UI References")]
        [SerializeField] private RoomInfoPanel roomInfoPanel;
        [SerializeField] private GameObject buildMenuPanel;

        [Header("Building Settings")]
        [SerializeField] private float gridSize = 1f;
        [SerializeField] private LayerMask buildableLayer;
        [SerializeField] private Color validPlacementColor = Color.green;
        [SerializeField] private Color invalidPlacementColor = Color.red;

        [Header("Resource Transport Settings")]
        [SerializeField] private float transportInterval = 1f;
        [SerializeField] private float maxTransportDistance = 10f;
        [SerializeField] private float transportSpeed = 5f;

        private void Awake()
        {
            InitializeManagers();
            ConfigureManagers();
            ConnectUI();
        }

        private void InitializeManagers()
        {
            // 确保所有管理器都存在
            if (buildingManager == null)
                buildingManager = FindObjectOfType<BuildingManager>();
            if (resourceManager == null)
                resourceManager = FindObjectOfType<ResourceManager>();
            if (roomPrefabManager == null)
                roomPrefabManager = FindObjectOfType<RoomPrefabManager>();
            if (resourceTransportManager == null)
                resourceTransportManager = FindObjectOfType<ResourceTransportManager>();

            // 如果仍然找不到，创建新的
            if (buildingManager == null)
                buildingManager = gameObject.AddComponent<BuildingManager>();
            if (resourceManager == null)
                resourceManager = gameObject.AddComponent<ResourceManager>();
            if (roomPrefabManager == null)
                roomPrefabManager = gameObject.AddComponent<RoomPrefabManager>();
            if (resourceTransportManager == null)
                resourceTransportManager = gameObject.AddComponent<ResourceTransportManager>();
        }

        private void ConfigureManagers()
        {
            // 配置建筑管理器
            if (buildingManager != null)
            {
                buildingManager.SetGridSize(gridSize);
                buildingManager.SetBuildableLayer(buildableLayer);
                buildingManager.SetPlacementColors(validPlacementColor, invalidPlacementColor);
            }

            // 配置资源传输管理器
            if (resourceTransportManager != null)
            {
                resourceTransportManager.SetTransportInterval(transportInterval);
                resourceTransportManager.SetMaxTransportDistance(maxTransportDistance);
                resourceTransportManager.SetTransportSpeed(transportSpeed);
            }
        }

        private void ConnectUI()
        {
            // 连接UI控制器
            if (buildingManager != null && roomInfoPanel != null)
            {
                buildingManager.SetRoomInfoPanel(roomInfoPanel);
            }

            // 初始化UI面板
            if (buildMenuPanel != null)
            {
                buildMenuPanel.SetActive(false);
            }
        }

        public void ToggleBuildMenu()
        {
            if (buildMenuPanel != null)
            {
                buildMenuPanel.SetActive(!buildMenuPanel.activeSelf);
            }
        }
    }
} 